import os, sys
import commands as syscmd
from optparse import OptionParser
def gen_rand_view(num = 10, viewname = 'view'):
    view_len = len(str(num))
    return [viewname + "%.*d" % (view_len, i) for i in range(0, num)]
def gen_rand_zone(num, zonename='zone'):
    if zonename.endswith('.'):
        zonename = zonename[0:-1]
    name_len = len(str(num))
    return [zonename + '%.*d' % (name_len, i)  + '.' for  i in range(num)]
def main(opts):
    if 'flog' not in opts:
        opts['flog'] = sys.stdout
    if 'zonenum' not in opts:
        opts['zonenum'] = 1
    if 'viewnum' not in opts:
        opts['viewnum'] = 0

    zonelist = gen_rand_zone(opts['zonenum']) 
    viewlist = gen_rand_view(opts['viewnum'])

    if not viewlist:
        for zone in zonelist:
            cmd = "conf_editor -c " + opts["conf"] + " -t add -p default -z " + zone + " -T master -f " + opts['path'] + '/' + zone + 'txt'
            ret, value = syscmd.getstatusoutput(cmd)
            print >>opts['flog'], 'command: ',cmd
            print >>opts['flog'], 'results: ',value
    else:
        for view in viewlist:
            for zone in zonelist:
                cmd = "conf_editor -c " + opts["conf"] + " -t add -p " + view + " -z " + zone + " -T master -f " + opts['path'] +'/' + view+ '/' + zone + 'txt'
                ret, value = syscmd.getstatusoutput(cmd)
                print >>opts['flog'], 'command: ',cmd
                print >>opts['flog'], 'results: ',value
if __name__ == "__main__":
    usagedoc = "usage: %prog [options]"
    parser = OptionParser(usage=usagedoc) 
    parser.add_option("-c", "--conf", dest = "conf")
    parser.add_option("-p", "--path", dest = "path")
    parser.add_option("-l", "--flog", dest = "flog", default=sys.stdout)
    parser.add_option("-z", "--zonenum", dest = "zonenum", default=1, type=int)
    parser.add_option("-v", "--viewnum", dest = "viewnum", default=0, type=int)
    parser.add_option("-Z", "--zoneprefix", dest = "zoneprefix", default='zone')
    parser.add_option("-V", "--viewprefix", dest = "viewprefix", default='view')
    (opts, args) = parser.parse_args()
    if (opts.conf == None):
        parser.print_help()
        sys.exit(0)
    main(vars(opts))


